<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/stuWelcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>选课管理</el-breadcrumb-item>
      <el-breadcrumb-item>立即选课</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="10">
      <!--搜索框-->
      <el-col :span="6">
        <el-input placeholder="按课程号查询" v-model="searchId" class="input-with-select" @keyup.enter.native="searchData1">
          <el-button slot="append" icon="el-icon-search" @click="searchdata1"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6" style="margin-left: 90px;margin-right: 90px;">
        <el-input placeholder="按课程名称查询" v-model="search" class="input-with-select" @keyup.enter.native="searchData2">
          <el-button slot="append" icon="el-icon-search" @click="searchdata"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6">
        <el-input placeholder="按课程性质查询" v-model="searchNature" class="input-with-select" @keyup.enter.native="searchData3">
          <el-button slot="append" icon="el-icon-search" @click="searchdata2"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <!--表格模块-->
    <el-table :data="selcourses" highlight-current-row
              ref='table'  border stripe height="300" style="margin-top: 30px;">
      <el-table-column type="index" fixed align="center"></el-table-column>
      <el-table-column prop="courseId" label="课程号"  align="center" width="120"></el-table-column>
      <el-table-column prop="courseName" label="课程名称" align="center" width="180"></el-table-column>
      <el-table-column prop="courseTeacher" label="任课教师" align="center"></el-table-column>
      <el-table-column prop="courseCredit" label="课程学分" align="center"></el-table-column>
      <el-table-column prop="week" label="起始周" align="center"></el-table-column>
      <el-table-column prop="courseNature" label="课程性质" align="center" width="130"></el-table-column>
      <el-table-column prop="location" label="上课地点" align="center"></el-table-column>
      <el-table-column prop="courseCollege" label="开课学院" align="center" width="150"></el-table-column>
      <el-table-column label="操作" fixed="right" width="120" align="center">
        <template slot-scope="scope">
          <!-- 选课 -->
          <el-tag
          type="primary"
            effect="dark"
            @click="open(scope.row)"
          >选课
          </el-tag>
          <!--退选 -->
          <el-tag
            type="warning"
            effect="dark"
            @click="drop(scope.row.courseId)"
          >退选
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!--分页插件-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="[2, 4, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>
  </el-card>

</template>

<script>
  import axios from 'axios';//axios
  import Qs from 'qs';//qs
  export default {
    name:"selcourse",
    //绑定数据
    data() {
      return {
        selcourses:[],//注意使用表格时，pop属性对应的内容等价于后端数据传输的字段(键)
        curpage:1,//当前页
        pagesize:4,//每页显示条数
        total:0,//总条数
        search:"",//模糊查询的内容
        searchId:"",
        searchNature:""
      }
    },
    //文档加载事件,当没有任何按钮去点击触发事件时，可以使用该函数在加载时，触发事件！
    mounted() {
      this.courseList();
    },
    methods:{
      selectRow(row) { // 给操作行 高亮
        if (row) {
          this.$refs.table.setCurrentRow(row, true);  // 高亮某一行
        } else {
          this.$refs.table.setCurrentRow();  // 取消高亮
        }
      },
      // 监听 pageSize 改变的事件
      handleSizeChange (newSize) {
        //   console.log(newSize)
        //  将监听接受到的每页显示多少条的数据 newSzie 赋值给 pagesize
        this.pagesize = newSize;
        //  修改完以后，重新发起请求获取一次数据
        this.courseList();
      },
      // 监听 页码值  改变的事件
      handleCurrentChange (newPage) {
        //   console.log(newPage)
        //  将监听接受到的页码值的数据 newPage 赋值给 pagenum
        this.curpage = newPage;
        //  修改完以后，重新发起请求获取一次数据
        this.courseList();
      },
      //执行分页查询的函数
      courseList(curpage = 1) {
        var courses=this;
        axios.get(
          "http://localhost:1313/selcourse/queryCourseByPage",
          {params:{current:courses.curpage, rows: courses.pagesize,search:courses.search}}
        ).then((res)=>{
          console.log(res)
          if (res.data.code == 200) {
            //将返回数据赋值给绑定数据，完成数据渲染
            this.selcourses = res.data.data.selcourses;
            this.total = res.data.data.total;
          } else {
            this.$message.error(res.data.msg)
          }
        });
      },
      // 选课
      open(data){
        // 获取到当前行数据
        console.log(data);
        console.log(data.courseId);
        // 首先判断是否已经选过该课程，若已经选过，就提示“已经选过该课程”，否则正常进行选课操作
        axios.get(
          "http://localhost:1313/selection/selectSelectionByCouId",
          {params:{couId:data.courseId}}
        ).then((res)=>{
          console.log(res)
          if (res.data.code == 200) {
            // 可以正常选课
            this.$confirm('此操作将表示您将选择该课程, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // 完成选课业务逻辑
              // 获取学生学号
              let stuNum = localStorage.getItem("stuNum");
              //发送axios请求
              axios({
                headers:{'Content-Type':'application/x-www-form-urlencoded'},
                method:"post",
                url:"http://localhost:1313/selection/selectionInsert",
                data:Qs.stringify({stuNum:stuNum,couId:data.courseId,couName:data.courseName,couTeacher:data.courseTeacher,couNature:data.courseNature,couCredit:data.courseCredit,week:data.week,location:data.location})
              }).then((res)=>{
                console.log(res)
                if (res.data.code == 200) {
                  //1.提示消息，是否添加成功
                  this.$message({
                    type: 'success',
                    message: '选课成功!'
                  });
                } else {
                  this.$message.error(res.data.msg)
                }
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消选课！'
              });
            });
          } else {
            this.$message.error(res.data.msg)
          }
        });
      },
      // 退选:删除该条选课记录
      drop(data){
        var selId;
        // 发起请求获取当前选课selId，通过selId实现退选
        axios.get(
          "http://localhost:1313/selection/selectSelIdByCouId",
          {params:{couId:data}}
        ).then((res)=>{
          console.log(res)
          if (res.data.code == 200) {
            // 获取到当前选课selId
            selId = res.data.data;
            console.log(selId);
            // 发起请求，实现退选
            axios.get(
              "http://localhost:1313/selection/selectionDelete",
              {params:{selId:selId}}
            ).then((res1)=>{
              console.log(res1)
              if (res.data.code == 200) {
                // 提示信息
                this.$message({
                  type: 'success',
                  message: res1.data.msg
                });
              } else {
                this.$message.error(res1.data.msg)
              }
            });
          } else {
            this.$message.error(res.data.msg)
          }
        });
      },

      //模糊查询
      searchdata(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
        this.courseList();
        this.search ="";
      },
      // 根据课程号完成查询
      courseList1(){
        var courses=this;
        axios.get(
          "http://localhost:1313/selcourse/queryCourseByIdPage",
          {params:{current:courses.curpage, rows: courses.pagesize,search:courses.searchId}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.selcourses = res.data.data.selcourse;
          this.total = res.data.data.total;
        });
      },
      // 根据课程性质完成查询
      courseList2(){
        var courses=this;
        axios.get(
          "http://localhost:1313/selcourse/queryCourseByNaturePage",
          {params:{current:courses.curpage, rows: courses.pagesize,search:courses.searchNature}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.selcourses = res.data.data.selcourses;
          this.total = res.data.data.total;
        });
      },
      searchdata1(){
        this.curpage = 1;
        this.courseList1();
        this.searchId ="";
      },
      searchdata2(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
       this.courseList2();
        this.searchNature ="";
      },
      // 按下回车键实现搜索
      searchData1(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata1();
        }
      },
      // 按下回车键实现搜索
      searchData2(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.curpage = 1;
          //回调分页查询，增加分页查询的参数
          this.courseList();
          this.search ="";
        }
      },
      // 按下回车键实现搜索
      searchData3(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata2();
        }
      }
    }
  }

</script>

<style>
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color:  rgba(227, 189, 241, 0.76) !important;
    color: #fff;
  }
  .el-input--suffix .el-input__inner {
    padding-right: 15px;
  }
  .form{
    display: grid;
    justify-content: center;
    /* align-items: center; */
    justify-items: center;
  }
  .el-button--info.is-plain {
    color: #704141;
    background: #f4f4f5;
    border-color: #d3d4d6;
  }
  .el-button--mini {
    font-size: 14px;
    border-radius: 3px;
  }
  .el-breadcrumb{
    padding-bottom: 20px;
  }
  .el-pagination{
    padding-top: 20px;
  }
</style>
